<?php
/**
 * Plugin Name: Rss Picker
 * Plugin URI: http://test.local
 * Description: Rss Picker is the best.
 * Version: 1.0
 * Author: Sergey Shadrin
 * Author URI: http://test.local
 * License: GPLv2 or later
 */

/*  Copyright YEAR  PLUGIN_AUTHOR_NAME  (email : PLUGIN AUTHOR EMAIL)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

define("PLUGIN_DB_PREFIX", "rsspckr_");

require_once("includes/common.inc.php");

function jal_install()
{
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

    $rssInfoTableName = Util::GetDatabaseTableName("rss_info");
    $sql = "CREATE TABLE $rssInfoTableName (
      `rss_info_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `rss_source_url` varchar(255) NOT NULL,
      `last_modified_date` datetime DEFAULT NULL,
      `title` varchar(255) DEFAULT NULL,
      `description` text,
      `link` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`rss_info_id`),
      UNIQUE KEY `rss_source_url` (`rss_source_url`)
    ) DEFAULT CHARSET=utf8;";
    dbDelta( $sql );

    $rssPostTableName = Util::GetDatabaseTableName("rss_post");
    $sql = "CREATE TABLE $rssPostTableName (
              `post_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
              `rss_info_id` int(10) unsigned NOT NULL,
              `title` varchar(255) DEFAULT NULL,
              `description` text,
              `content` text,
              `link` varchar(255) NOT NULL,
              `author` varchar(255) DEFAULT NULL,
              `creation_date` datetime DEFAULT NULL,
              PRIMARY KEY (`post_id`),
              UNIQUE KEY `link` (`link`),
              KEY `fk_rss_post_info` (`rss_info_id`),
              CONSTRAINT `fk_rss_post_info` FOREIGN KEY (`rss_info_id`) REFERENCES $rssInfoTableName (`rss_info_id`) ON DELETE CASCADE
            ) DEFAULT CHARSET=utf8;";
    dbDelta( $sql );

    $rssPostImageTableName = Util::GetDatabaseTableName('rss_post_image');
    $sql = "CREATE TABLE $rssPostImageTableName (
              `post_image_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
              `title` varchar(255) DEFAULT NULL,
              `link` varchar(255) NOT NULL,
              `image` blob,
              `post_id` int(10) unsigned NOT NULL,
              PRIMARY KEY (`post_image_id`),
              UNIQUE KEY `link` (`link`),
              KEY `fk_rss_post_image_post` (`post_id`),
              CONSTRAINT `fk_rss_post_image_post` FOREIGN KEY (`post_id`) REFERENCES $rssPostTableName (`post_id`) ON DELETE CASCADE
            ) DEFAULT CHARSET=utf8;";
    dbDelta( $sql );

    $rssTagTableName = Util::GetDatabaseTableName('rss_tag');
    $sql = "CREATE TABLE $rssTagTableName (
              `rss_tag_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
              `title` varchar(255) DEFAULT NULL,
              PRIMARY KEY (`rss_tag_id`),
              UNIQUE KEY `title` (`title`)
            ) DEFAULT CHARSET=utf8;";
    dbDelta($sql);

    $rssPostTagTableName = Util::GetDatabaseTableName('rss_post_tag');
    $sql = "CREATE TABLE $rssPostTagTableName (
              `rss_post_tag_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
              `rss_post_id` int(10) unsigned NOT NULL,
              `rss_tag_id` int(10) unsigned NOT NULL,
              PRIMARY KEY (`rss_post_tag_id`),
              KEY `fk_rss_post_tag_rss_post` (`rss_post_id`),
              KEY `fk_rss_post_tag_rss_tag` (`rss_tag_id`),
              CONSTRAINT `fk_rss_post_tag_rss_post` FOREIGN KEY (`rss_post_id`) REFERENCES $rssPostTableName (`post_id`) ON DELETE CASCADE,
              CONSTRAINT `fk_rss_post_tag_rss_tag` FOREIGN KEY (`rss_tag_id`) REFERENCES $rssTagTableName (`rss_tag_id`) ON DELETE CASCADE
            ) DEFAULT CHARSET=utf8;";
    dbDelta($sql);
}

function jal_install_data()
{
    global $wpdb;
    $welcome_name = "Mr. WordPress";
    $welcome_text = "Congratulations, you just completed the installation!";
    $table_name = $wpdb->prefix . "liveshoutbox";

    $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'name' => $welcome_name, 'text' => $welcome_text ) );
}

register_activation_hook( __FILE__, 'jal_install' );
//register_activation_hook( __FILE__, 'jal_install_data' );

function my_scripts_method() {
    $pluginDirUrl = plugin_dir_url( __FILE__ );
    wp_enqueue_style(
        'custom_wp_admin_css',
        $pluginDirUrl . 'css/admin-style.css'
    );
    wp_enqueue_script('jquery');
    wp_enqueue_script(
        'script',
        $pluginDirUrl . 'js/script.js',
        array( 'jquery' )
    );
}

if( is_admin() )
{
    $my_settings_page = new MySettingsPage();
    add_action( 'admin_enqueue_scripts', 'my_scripts_method' );
}

